//
//  CommentBookViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-2-19.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "CommentBookViewController.h"

@interface CommentBookViewController ()

@end

@implementation CommentBookViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        lastRate = 10;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(commentResult:) name:gotCommentBookNotify object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIPanGestureRecognizer *pan = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rateAction:)] autorelease];
    [starsView addGestureRecognizer:pan];
    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rateAction:)] autorelease];
    [starsView addGestureRecognizer:tap];
    [self setRate:lastRate];
    
    [commentTextView becomeFirstResponder];
}

- (void)rateAction:(UIGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:gesture.view];
    int rate;
    if (location.x <= 18) {
        rate = 0;
    }
    else if (location.x > 178) {
        rate = 10;
    }
    else {
        rate = (location.x - 18) / 16;
    }
    if (rate != lastRate) {
        lastRate = rate;
        [self setRate:rate];
    }
}

- (void)setRate:(int)rate
{
    for (int i = 0; i < 5; i++) {
        UIImageView *starView = (UIImageView *)[[starsView subviews] objectAtIndex:i];
        if (i * 2 + 2 <= rate) {
            starView.image = [UIImage imageNamed:@"fullStar.png"];
        }
        else if (i * 2 + 1 <= rate) {
            starView.image = [UIImage imageNamed:@"halfStar.png"];
        }
        else {
            starView.image = [UIImage imageNamed:@"emptyStar.png"];
        }
    }
}

- (IBAction)commitComment:(id)sender
{
    [commentTextView resignFirstResponder];
    if (commentTextView.text.length <= 5) {
        ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"多写一点吧"] autorelease];
        [promptView show];
        return;
    }
    
    [self showLoadingView];
    [[DataRequest sharedRequest] commentBookWithID:_itemid comment:commentTextView.text rate:lastRate];
}

- (void)commentResult:(NSNotification *)notification
{
    [self removeloadingView];
    if ([notification.object isKindOfClass:[NSDictionary class]]) {
        NSArray *result = [notification.object valueForKey:@"result"];
        if ([result isKindOfClass:[NSDictionary class]]) {
            if ([[result valueForKey:@"state_code"] intValue] == 1) {
                [[NSNotificationCenter defaultCenter] postNotificationName:kUpdateComment object:nil];
                UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
                [navController popViewControllerAnimated:YES];
                
                ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"发送成功"] autorelease];
                [promptView show];
            }
            else {
                ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"发送失败"] autorelease];
                [promptView show];
            }
        }
    }
}

- (IBAction)navBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:contentView.bounds] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_itemid release];
    [_itemname release];
    [starsView release];
    [commentTextView release];
    [commitButton release];
    [contentView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [starsView release];
    starsView = nil;
    [commentTextView release];
    commentTextView = nil;
    [commitButton release];
    commitButton = nil;
    [contentView release];
    contentView = nil;
    [super viewDidUnload];
}
@end
